//  This file is part of Swordbible - http://www.swordwarrior.net
//  Copyright (C) 2003-2008 Manu Mital mital.manu@gmail.com

//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2, or (at your option)
//  any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software Foundation,
//  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "SwordBible.h"
#include "MainFrm.h"
#include "BibleTree.h"
#include "BibleForm.h"
#include ".\mainfrm.h"
#include "Remove.h"
#include "SwordBibleView.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


CVerseLists* CMainFrame::m_pModelessVL = NULL;

// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	ON_WM_CREATE()
	ON_COMMAND(ID_TOP_MAX, OnTopMax)
	ON_COMMAND(ID_BOTTOM_MAX, OnBottomMax)
	ON_COMMAND(ID_NORMAL, OnNormal)
	ON_WM_SIZE()
	ON_COMMAND(ID_DELETE_BOOKMARK, OnDeleteBookmark)
	ON_COMMAND(ID_ADD_BOOKMARK, OnAddBookmark)
	ON_COMMAND_RANGE(ID_BOOKMARK_STRING + 3,ID_BOOKMARK_STRING + 2 + MAXBOOKMARKS, OnSubMenuBookMarks)
	ON_COMMAND(ID_BIBLEPAGE_SEARCH, OnMenuBiblepageSearch)
	ON_COMMAND(ID_BOTTOM_SEARCH, OnBottomSearch)
	ON_COMMAND(ID_BIBLE_DISPLAY,OnMainDisplay)
	ON_COMMAND(ID_BOTTOM_DISPLAY,OnBottomDisplay)
	ON_COMMAND(ID_VIEW_DEVOTIONAL, OnViewDevotional)
	ON_COMMAND(ID_VERSELISTS, OnVerseLists)
	ON_COMMAND(ID_WEB, OnWeb)
	ON_COMMAND(ID_INSTALLMGR, OnInstallMgr)
	ON_COMMAND(ID_PRAYERREQUEST, OnPrayerrequest)

	ON_UPDATE_COMMAND_UI(ID_VIEW_QUICKVERSE_TOOLBAR, OnUpdateControlBarMenu)
	ON_COMMAND_EX(ID_VIEW_QUICKVERSE_TOOLBAR, OnBarCheck)


	// lemma tags
	ON_COMMAND(ID_LEMMA, OnLemma)
	ON_UPDATE_COMMAND_UI(ID_LEMMA, OnUpdateLemma)

	// morph tags
	ON_COMMAND(ID_MORPH, OnMorph)
	ON_UPDATE_COMMAND_UI(ID_MORPH, OnUpdateMorph)
	
	// footnotes
	ON_COMMAND(ID_FOOTNOTES, OnFootnotes)
	ON_UPDATE_COMMAND_UI(ID_FOOTNOTES, OnUpdateFootnotes)
	
	// cross-references
	ON_COMMAND(ID_CROSSREF, OnCrossref)
	ON_UPDATE_COMMAND_UI(ID_CROSSREF, OnUpdateCrossref)
	
	// preverse heading
	ON_COMMAND(ID_HEADINGS, OnHeading)
	ON_UPDATE_COMMAND_UI(ID_HEADINGS, OnUpdateHeading)

		// preverse heading
	ON_COMMAND(ID_REDCHRIST, OnRed)
	ON_UPDATE_COMMAND_UI(ID_REDCHRIST, OnUpdateRed)






	ON_MESSAGE(WM_SHOW_DEVOTIONAL, OnShowDevotional)
	ON_MESSAGE(WM_LOAD_XREF_BIBLEVERSIONS, OnXrefBibleVersions)

	ON_COMMAND_RANGE(ID_BIBLEVERSION_NAMES,ID_BIBLEVERSION_NAMES + nBibles , OnChangeXrefVersion)

   ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipNotify)
   ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipNotify)

	ON_WM_DESTROY()

	ON_COMMAND(ID_MAINFRAME_PRINT, OnMainFramePrint)
	ON_COMMAND(ID_MAINFRAME_DISPLAY, OnMainFrameDisplay)
	ON_COMMAND(ID_MAINFRAME_SEARCH, OnMainFrameSearch)

	ON_COMMAND(ID_BIBLE_PRINT, &CMainFrame::OnMainBiblePrint)
	ON_COMMAND(ID_BIBLE_COPY, &CMainFrame::OnMainBibleCopy)
	ON_COMMAND(ID_BIBLE_SELECTALL, &CMainFrame::OnMainBibleSelectall)
	ON_COMMAND(ID_BIBLEPAGE_COPYMANAGER, &CMainFrame::OnBiblepageCopymanager)
	ON_COMMAND(ID_BIBLE_ZOOMTEXT, &CMainFrame::OnMainBibleZoomtext)
	ON_COMMAND(ID_BIBLE_SPLITVIEW, &CMainFrame::OnBibleSplitview)
	ON_COMMAND(ID_BIBLE_PREVIOUSCHAPTER, &CMainFrame::OnMainBiblePrevious)
	ON_COMMAND(ID_BIBLE_NEXTCHAPTER, &CMainFrame::OnMainBibleNext)
	ON_UPDATE_COMMAND_UI(ID_BIBLE_ZOOMTEXT, &CMainFrame::OnUpdateBibleZoom)
	ON_COMMAND(ID_COMMENTARY_COPY, &CMainFrame::OnCommentaryCopy)
	ON_COMMAND(ID_COMMENTARY_SELECTALL, &CMainFrame::OnMFCommentarySelectall)
	ON_COMMAND(ID_COMMENTARY_ZOOMTEXT, &CMainFrame::OnMFCommentaryZoomtext)
	ON_UPDATE_COMMAND_UI(ID_COMMENTARY_ZOOMTEXT, &CMainFrame::OnMFUpdateCommentaryZoom)
	ON_COMMAND(ID_COMMENTARY_PREVIOUSCHAPTER, &CMainFrame::OnMFCommentaryPrevious)
	ON_COMMAND(ID_COMMENTARY_NEXTCHAPTER, &CMainFrame::OnMFCommentaryNext)
	ON_COMMAND(ID_COMMENTARY_PRINT, &CMainFrame::OnMFCommentaryPrint)
	ON_COMMAND(ID_COMMENTARY_SYNCHRONIZE, &CMainFrame::OnMFCommentarySynch)
	ON_UPDATE_COMMAND_UI(ID_COMMENTARY_SYNCHRONIZE, &CMainFrame::OnUpdateMFCommentarySynch)
	ON_UPDATE_COMMAND_UI(ID_BIBLE_SPLITVIEW, &CMainFrame::OnUpdateBibleSplitview)
	ON_COMMAND(ID_NOTES_PREVIOUSCHAPTER, &CMainFrame::OnNotesPrevious)
	ON_COMMAND(ID_NOTES_NEXTCHAPTER, &CMainFrame::OnNotesNext)
	ON_COMMAND(ID_NOTES_SAVE, &CMainFrame::OnNotesSave)
	ON_COMMAND(ID_NOTES_PRINT, &CMainFrame::OnNotesPrint)
	ON_COMMAND(ID_NOTES_SYNCHRONIZE, &CMainFrame::OnNotesSynchronize)
	ON_COMMAND(ID_NOTES_CUT, &CMainFrame::OnNotesCut)
	ON_COMMAND(ID_NOTES_COPY, &CMainFrame::OnNotesCopy)
	ON_COMMAND(ID_NOTES_PASTE, &CMainFrame::OnNotesPaste)
	ON_COMMAND(ID_NOTES_BOLD, &CMainFrame::OnNotesBold)
	ON_UPDATE_COMMAND_UI(ID_NOTES_BOLD, &CMainFrame::OnUpdateNotesBold)
	ON_COMMAND(ID_NOTES_ITALIC, &CMainFrame::OnNotesItalic)
	ON_UPDATE_COMMAND_UI(ID_NOTES_ITALIC, &CMainFrame::OnUpdateNotesItalic)
	ON_UPDATE_COMMAND_UI(ID_NOTES_UNDERLINE, &CMainFrame::OnUpdateNotesUnderline)
	ON_COMMAND(ID_NOTES_LEFTALIGN, &CMainFrame::OnNotesLeftalign)
	ON_UPDATE_COMMAND_UI(ID_NOTES_LEFTALIGN, &CMainFrame::OnUpdateNotesLeftalign)
	ON_COMMAND(ID_NOTES_CENTERALIGN, &CMainFrame::OnNotesCenteralign)
	ON_UPDATE_COMMAND_UI(ID_NOTES_CENTERALIGN, &CMainFrame::OnUpdateNotesCenteralign)
	ON_COMMAND(ID_NOTES_RIGHTALIGN, &CMainFrame::OnNotesRightalign)
	ON_UPDATE_COMMAND_UI(ID_NOTES_RIGHTALIGN, &CMainFrame::OnUpdateNotesRightalign)
	ON_COMMAND(ID_NOTES_INSERTBULLETS, &CMainFrame::OnNotesInsertbullets)
	ON_UPDATE_COMMAND_UI(ID_NOTES_SYNCHRONIZE, &CMainFrame::OnUpdateNotesSynchronize)
	ON_COMMAND(ID_NOTES_UNDERLINE, &CMainFrame::OnNotesUnderline)
	
	ON_COMMAND(ID_DICTIONARY_PRINT, &CMainFrame::OnDictionaryPrint)
	ON_COMMAND(ID_DICTIONARY_COPY, &CMainFrame::OnDictionaryCopy)
	ON_COMMAND(ID_DICTIONARY_SELECTALL, &CMainFrame::OnDictionarySelectall)
	ON_COMMAND(ID_DICTIONARY_DISPLAY, &CMainFrame::OnBottomleftDisplay)
	ON_COMMAND(ID_DICTIONARY_PREVIOUSENTRY, &CMainFrame::OnDictionaryPrevious)
	ON_COMMAND(ID_DICTIONARY_NEXTENTRY, &CMainFrame::OnDictionaryNext)
	ON_COMMAND(ID_DICTIONARY_ZOOMTEXT, &CMainFrame::OnDictionaryZoomtext)
	ON_UPDATE_COMMAND_UI(ID_DICTIONARY_ZOOMTEXT, &CMainFrame::OnUpdateDictionaryZoom)

	ON_COMMAND(ID_GENBOOKS_PRINT, &CMainFrame::OnGenBooksPrint)
	ON_COMMAND(ID_GENBOOKS_COPY, &CMainFrame::OnGenBooksCopy)
	ON_COMMAND(ID_GENBOOKS_SELECTALL, &CMainFrame::OnGenBooksSelectall)
	ON_COMMAND(ID_GENBOOKS_DISPLAY, &CMainFrame::OnGenBooksDisplay)
	ON_COMMAND(ID_GENBOOKS_PREVIOUS, &CMainFrame::OnGenBooksPrevious)
	ON_COMMAND(ID_GENBOOKS_NEXT, &CMainFrame::OnGenBooksNext)
	ON_COMMAND(ID_GENBOOKS_ZOOMTEXT, &CMainFrame::OnGenBooksZoomtext)
	ON_UPDATE_COMMAND_UI(ID_GENBOOKS_ZOOMTEXT, &CMainFrame::OnUpdateGenBooksZoom)
	
	
	
	
	ON_COMMAND(ID_OPTIONS_REMEMBERLAST, &CMainFrame::OnOptionsRememberlast)
	ON_UPDATE_COMMAND_UI(ID_OPTIONS_REMEMBERLAST, &CMainFrame::OnUpdateRememberlast)
	ON_COMMAND(ID_OPTIONS_DEVOTIONAL, &CMainFrame::OnOptionsDevotional)
	ON_UPDATE_COMMAND_UI(ID_OPTIONS_DEVOTIONAL, &CMainFrame::OnUpdateOptionsDevotional)
	ON_WM_CLOSE()

	ON_COMMAND(ID_QUICKVERSE_GO, OnQuickVerseGo)

	
	END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};


// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	m_bPreview = FALSE;
	m_pModeless = NULL;
	m_pModelessVL = NULL;

}

CMainFrame::~CMainFrame()
{
	

	
}


int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT , WS_CHILD |
        WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS |
         CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||

       !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
   {
      TRACE0("Failed to create toolbar\n");
      return -1;      // fail to create
   }


	if (!m_wndQuickVerseBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
	| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC, CRect(0, 0, 0, 0), ID_VIEW_QUICKVERSE_TOOLBAR) ||
	!m_wndQuickVerseBar.LoadToolBar(IDR_MAINFRAME_QUICKVERSE))
	{
	TRACE0("Failed to create toolbar\n");
	return -1; // fail to create
	}

	// create the combo box for the quick verse toolbar
	CRect rect;
	m_wndQuickVerseBar.GetItemRect( m_wndQuickVerseBar.CommandToIndex(ID_DUMMY1), rect);
	rect.right += (rect.Width() * 6); // Using 7 dummy buttons for space.
	rect.top =  3;      //(((rect.Height() - GetSystemMetrics(SM_CYMENU)) + 1) / 2); // + 3;
	rect.bottom += 260;
	rect.left += 5;

	// Create the combo box!
	if(!QuickVerseCombo.Create(CBS_DROPDOWN | WS_VISIBLE | WS_CHILD, rect, &m_wndQuickVerseBar, IDC_COMBO_QUICKVERSE) )
	{
	TRACE("Failed to create combo-box\n");
	return false;
	}

	
	pFont = new CFont();  //<--- deleted in the destructor
	pFont->CreatePointFont(100,_T("Georgia"));
	QuickVerseCombo.SetFont(pFont);
	



	if (!m_wndStatusBar.Create(this) )
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}


		//load the application icon
	HICON icon = ::AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	SetIcon(icon, TRUE);

	m_wndToolBar.LoadTCToolBar(48, IDB_TOOLBAR_MAINFRAME,IDB_TOOLBAR_MAINFRAME,
                             IDB_TOOLBAR_MAINFRAME, RGB(255,128,255));
	
	m_wndQuickVerseBar.LoadTCToolBar(24, IDB_BITMAP_QUICKVERSE,IDB_BITMAP_QUICKVERSE,
                             IDB_BITMAP_QUICKVERSE, RGB(255,128,255));


	// TODO: Delete these three lines if you don't want the toolbar to be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	m_wndQuickVerseBar.EnableDocking(CBRS_ALIGN_ANY);
	
	EnableDocking(CBRS_ALIGN_ANY);
	
	DockControlBar(&m_wndToolBar);
	DockControlBar(&m_wndQuickVerseBar);

	


	// load the bookmarks from the registry
	LoadBookmarks();

	//Load the verse history in the quick verse toolbar
	LoadQuickHistory();

	

	return 0;
}

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,
	CCreateContext* pContext)
{

	// create the first splitter and the associated Tree view of the first column
	if(!m_wndSplitter.CreateStatic(this, 2, 1) )
		return false;


	if(!m_wndSplitter1.CreateStatic(&m_wndSplitter,1,2,WS_CHILD|WS_VISIBLE, m_wndSplitter.IdFromRowCol(0, 0)))
		return false;	


	// create the tree view
		if(!m_wndSplitter1.CreateView(0, 0,RUNTIME_CLASS(CBibleTree), CSize(0, 0), pContext))
		return false;

        // create the bible page view
		if(!m_wndSplitter1.CreateView(0, 1,RUNTIME_CLASS(CBibleForm), CSize(0, 0), pContext))
		return false;





	if(!m_wndSplitter2.CreateStatic(&m_wndSplitter,1,2,WS_CHILD|WS_VISIBLE, m_wndSplitter.IdFromRowCol(1, 0)))
		return false;	

	// create the bottom view
    if (!m_wndSplitter2.CreateView(0,1 ,RUNTIME_CLASS(CBibleForm), CSize(0, 0), pContext))
	return false;

    // create the bottom left view
	if(!m_wndSplitter2.CreateView(0, 0,RUNTIME_CLASS(CBibleForm), CSize(0, 0), pContext))
		return false;

	OnNormal();

	return true;	// return
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	cs.style &= ~FWS_ADDTOTITLE;

	return TRUE;
}

BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
	if( (pMsg->message == WM_KEYDOWN) 
		&& (pMsg->wParam == VK_RETURN) 
		&&	 GetFocus()->GetSafeHwnd() == QuickVerseCombo.GetFocus()->GetSafeHwnd() )
	{
		OnQuickVerseGo();
		return TRUE;
	}


	return CFrameWnd::PreTranslateMessage(pMsg);
}


// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

void CMainFrame::OnTopMax()

{
	

// Get the size of the window containing the splitter views
	CRect totalSize;
	GetWindowRect( &totalSize);

    m_wndSplitter.SetRowInfo(0,totalSize.Height() ,0 );
	m_wndSplitter.RecalcLayout();


	m_wndSplitter1.SetColumnInfo(0, 150, 150);
	m_wndSplitter1.RecalcLayout();




}

void CMainFrame::OnBottomMax()

{
	// Get the size of the window containing the splitter views
	CRect totalSize;
	GetWindowRect(&totalSize);

	m_wndSplitter.SetRowInfo(0,0 ,0);
	m_wndSplitter.RecalcLayout();

    m_wndSplitter.SetRowInfo(1,totalSize.Height() ,0);
	m_wndSplitter.RecalcLayout();

	m_wndSplitter2.SetColumnInfo(0, 0, 0);
	m_wndSplitter2.RecalcLayout();





}

void CMainFrame::OnNormal()

{
	CRect totalSize;
	//GetWindowRect(totalSize);
	SystemParametersInfo(SPI_GETWORKAREA,0,&totalSize,0);

	m_wndSplitter.SetRowInfo(0,  totalSize.Height() /1.85,0);
	m_wndSplitter.RecalcLayout();

	m_wndSplitter1.SetColumnInfo(0, totalSize.Width()/6, 0);
	m_wndSplitter1.RecalcLayout();

	
	m_wndSplitter2.SetColumnInfo(0, totalSize.Width()/2.6, 0);
	m_wndSplitter2.RecalcLayout();


}




// CMainFrame message handlers


void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
	CFrameWnd::OnSize(nType, cx, cy);




	// TODO: Add your message handler code here
}

void CMainFrame::OnDeleteBookmark()
{

	CRemove dlg;
	dlg.icon = "file";
	dlg.type = "multiple";
	dlg.arr.RemoveAll();

	
	 CString listStr = AfxGetApp()->GetProfileString("BOOKMARKS", "list", "");
	 sword::VerseKey vk;
	 sword::ListKey lk = vk.ParseVerseList(listStr, vk , TRUE);


	 for(lk= sword::TOP; !lk.Error(); lk++)   dlg.arr.Add( (const char *) lk );

	

	if( dlg.DoModal() == IDOK)
	{
		AfxGetApp() ->WriteProfileString("BOOKMARKS", "list", dlg.modList);
		LoadBookmarks();

	}



}


// add a new bookmark
void CMainFrame::OnAddBookmark()
{
	CMenu *pMainMenu = GetMenu();
	CMenu *pSubMenu = pMainMenu->GetSubMenu(bmmi); 


	CString BookMark;  // this is the bookmark that needs to be added
	BookMark.Format("%s %d:%d",CSwordBibleDoc::key.getBookAbbrev(),CSwordBibleDoc::Chapter(), CSwordBibleDoc::Verse());
	sword::SWKey bmKey = BookMark;
	sword::SWKey existing;

	int numSubMenuEntries = pSubMenu ->GetMenuItemCount();


	// make sure that the bookmark to be added does not already exist
	for(int  i = 0; i < numSubMenuEntries; i++)
    {
		CString subMenuChild;
		pSubMenu->GetMenuString(i,subMenuChild,MF_BYPOSITION);
		existing = subMenuChild;

		if(existing == bmKey)
		return;                          // return if bookmark already exists


	}

		// write the new item in the registry
	    CString list = AfxGetApp()->GetProfileString("BOOKMARKS", "list", "");
		list = list + ";" + BookMark;
		AfxGetApp() ->WriteProfileString("BOOKMARKS", "list", list);

		// Add bookmark to the mainframe menu
		int zero_based_pos = ( pSubMenu->GetMenuItemCount() );
		pSubMenu ->AppendMenu(MF_STRING,ID_BOOKMARK_STRING + zero_based_pos, BookMark);
	

}


// this function is called when a user clicks on a bookmark from the 
// main menu
void CMainFrame::OnSubMenuBookMarks(UINT nID)
{

	CMenu *pMainMenu = GetMenu();

	CMenu *pSubMenu = pMainMenu->GetSubMenu(bmmi); // Bookmark is submenu 8

	int id = int(nID)- int(ID_BOOKMARK_STRING);

	CString bookmarkStr;

    pSubMenu->GetMenuString(id, bookmarkStr, MF_BYPOSITION);

	CSwordBibleDoc::key = bookmarkStr;


   //SendMessageToDescendants(WM_TREE_SETSEL,0,0);  <-- don't call this if we want to highlight bookmarked verse

	 /* the new key has been set. Change the display in the bible, and if synchronize is TRUE, change the 
       display in the notes/commentary */

	CBiblePage::ActiveMainFrameCommand = "ShowChapter";
	SendMessageToDescendants(ID_BIBLEPAGE_COMMAND, 0,0);

	if(CSwordBibleDoc::notesSynch)
	{
	CNotesPage::ActiveMainFrameCommand = "BibleSynch";
	SendMessageToDescendants(ID_NOTES_COMMAND,0,0);
	}

	if(CSwordBibleDoc::bottomSynch)
	{
	CBottomPage::ActiveMainFrameCommand = "BibleSynch";
	SendMessageToDescendants(ID_COMMENTARY_COMMAND,0,0);
	}



	// highlight the verse in the bible window
	CBiblePage::ActiveMainFrameCommand = "highlight";
	SendMessageToDescendants(ID_BIBLEPAGE_COMMAND,0,0);
}







// load the bookmarks from the registry into the mainframe  menu
void CMainFrame::LoadBookmarks(void)
{
	
	CMenu *pMainMenu = GetMenu();
	CMenu *pSubMenu = pMainMenu->GetSubMenu(bmmi); // Bookmark is submenu 8

	int numSubMenuEntries = pSubMenu ->GetMenuItemCount();

	int i = numSubMenuEntries - 1;

	while(i > 2) pSubMenu->RemoveMenu(i--, MF_BYPOSITION);  // remove all existing bookmarks

	// get bookmarks from the registry and add them	
	 CString listStr = AfxGetApp()->GetProfileString("BOOKMARKS", "list", "");
	 sword::VerseKey vk;
	 sword::ListKey lk = vk.ParseVerseList(listStr, vk , TRUE);

	
	 for(lk= sword::TOP; !lk.Error(); lk++)
	 {
		 i++;
		 pSubMenu ->AppendMenu(MF_STRING,ID_BOOKMARK_STRING + i, (const char *) lk  );
	 }


}

// load the bible versions in the mainframe menu so user can choose which version to use
// for cross-references
LRESULT  CMainFrame::OnXrefBibleVersions(WPARAM wParam, LPARAM lParam)
{

	
	CMenu *pPopup = GetMenu()->GetSubMenu(1)->GetSubMenu(4); 


	int dwSelectionMade; 

	if(CSwordBibleDoc::pXrefModuleActive == NULL) return 0L;

	CString n1 = CSwordBibleDoc::pXrefModuleActive->Name();

 
   // fill in the strings for different bible versions
	for(int i = 0; i < CSwordBibleDoc::numBiblesAvailable; i++)
		if(CSwordBibleDoc::pRTFBibleModule[i] != NULL)
		{
			         CString n2 = CSwordBibleDoc::pRTFBibleModule[i]->Name();

			         pPopup->AppendMenu(MF_STRING,ID_BIBLEVERSION_NAMES + i, (CString) CSwordBibleDoc::pRTFBibleModule[i]->Description());
					 if( n1 == n2 ) pPopup->CheckMenuItem(ID_BIBLEVERSION_NAMES + i,MF_CHECKED); 

		}


  	

	return 0L;

}

void CMainFrame::OnChangeXrefVersion(UINT nID)
{
	CMenu *pSubMenu = 	GetMenu()->GetSubMenu(1)->GetSubMenu(4); 

    int active = int(nID)- int(ID_BIBLEVERSION_NAMES);  // set the new active version
	CSwordBibleDoc::pXrefModuleActive = CSwordBibleDoc::pRTFBibleModule[active];   // set the active module
     
	for(int i = 0; i < CSwordBibleDoc::numBiblesAvailable; i++)
		if(CSwordBibleDoc::pRTFBibleModule[i] != NULL)
		    if(active == i)			pSubMenu->CheckMenuItem(ID_BIBLEVERSION_NAMES + i,MF_CHECKED); 
			else    				pSubMenu->CheckMenuItem(ID_BIBLEVERSION_NAMES + i,MF_UNCHECKED); 


		
	// write the active module for cross-ref tooltips to the registry
	CString name = CSwordBibleDoc::pXrefModuleActive->Name();
	AfxGetApp()->WriteProfileString("Tags", "XrefModule", name);

	


}


void CMainFrame::OnViewDevotional()
{
	 // Display the dialog box
    if (!m_pModeless)
        m_pModeless = new CDevotional;

   if (!::IsWindow(m_pModeless->GetSafeHwnd()))
        m_pModeless->Create(IDD_DEVOTIONAL, this);

    m_pModeless->ShowWindow(SW_SHOW); 

}


void CMainFrame::OnVerseLists()
{
	 // Display the dialog box
    if (!m_pModelessVL)
       m_pModelessVL= new CVerseLists;

   if (!::IsWindow(m_pModelessVL->GetSafeHwnd()))
       m_pModelessVL->Create(IDD_VERSELISTS, this);

    m_pModelessVL->ShowWindow(SW_SHOW);

}





LRESULT CMainFrame::OnShowDevotional(WPARAM wParam, LPARAM lParam)
{
	// TODO: Add your command handler code here

OnViewDevotional();

	return 0L;
}



void CMainFrame::OnWeb()
{
		ShellExecute(NULL, "open", "http://www.swordwarrior.net/modules.html",
		NULL, NULL, SW_SHOWNORMAL);

}

void CMainFrame::OnInstallMgr()
{
	
		STARTUPINFO si;
PROCESS_INFORMATION pi;
memset(&pi, 0, sizeof(pi));
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
CreateProcess( 0, "InstallManager.exe" ,
  0, 0, 0, 0, 0, 0, &si, &pi);

}


void CMainFrame::OnPrayerrequest()
{
		ShellExecute(NULL, "open", "mailto:dwellingplace77@hotmail.com?SUBJECT=Prayer Request",
		NULL, NULL, SW_SHOWNORMAL);
}

// turn the strong's numbers on or off
void CMainFrame::OnLemma()
{
	CSwordBibleDoc::lemma_tags = !(CSwordBibleDoc::lemma_tags);

	// write setting to the registry
	AfxGetApp()->WriteProfileInt("Tags","lemma_tags", CSwordBibleDoc::lemma_tags);

	SendMessageToDescendants(WM_TOGGLE_TAGS,0,0);

	// redisplay 
 	CBiblePage::ActiveMainFrameCommand = "ShowChapter";
	SendMessageToDescendants(ID_BIBLEPAGE_COMMAND, 0,0);

	
	
}

void CMainFrame::OnUpdateLemma(CCmdUI * pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::lemma_tags);

}


void CMainFrame::OnMorph()

{
	CSwordBibleDoc::morph_tags = !(CSwordBibleDoc::morph_tags);

	// write setting to the registry
	AfxGetApp()->WriteProfileInt("Tags","morph_tags", CSwordBibleDoc::morph_tags);

	SendMessageToDescendants(WM_TOGGLE_TAGS,0,0);

	// redisplay 
 	CBiblePage::ActiveMainFrameCommand = "ShowChapter";
	SendMessageToDescendants(ID_BIBLEPAGE_COMMAND, 0,0);
	
}

void CMainFrame::OnUpdateMorph(CCmdUI * pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::morph_tags);

}





void CMainFrame::OnFootnotes()
{
	CSwordBibleDoc::fn_tags = !(CSwordBibleDoc::fn_tags);

	// write setting to the registry
	AfxGetApp()->WriteProfileInt("Tags","fn_tags", CSwordBibleDoc::fn_tags);

	SendMessageToDescendants(WM_TOGGLE_TAGS,0,0);

	// redisplay 
 	CBiblePage::ActiveMainFrameCommand = "ShowChapter";
	SendMessageToDescendants(ID_BIBLEPAGE_COMMAND, 0,0);
}

void CMainFrame::OnUpdateFootnotes(CCmdUI * pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::fn_tags);

}


void CMainFrame::OnCrossref()
{
	CSwordBibleDoc::crossref_tags = !(CSwordBibleDoc::crossref_tags);

	// write setting to the registry
	AfxGetApp()->WriteProfileInt("Tags","crossref_tags", CSwordBibleDoc::crossref_tags);

	SendMessageToDescendants(WM_TOGGLE_TAGS,0,0);

	// redisplay 
 	CBiblePage::ActiveMainFrameCommand = "ShowChapter";
	SendMessageToDescendants(ID_BIBLEPAGE_COMMAND, 0,0);
}

void CMainFrame::OnUpdateCrossref(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::crossref_tags);
	
}

void CMainFrame::OnHeading()
{
	CSwordBibleDoc::heading_tags = !(CSwordBibleDoc::heading_tags);

	// write setting to the registry
	AfxGetApp()->WriteProfileInt("Tags","heading_tags", CSwordBibleDoc::heading_tags);

	SendMessageToDescendants(WM_TOGGLE_TAGS,0,0);

// redisplay 
 	CBiblePage::ActiveMainFrameCommand = "ShowChapter";
	SendMessageToDescendants(ID_BIBLEPAGE_COMMAND, 0,0);
}

void CMainFrame::OnUpdateHeading(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::heading_tags);
	
}

void CMainFrame::OnRed()
{
	CSwordBibleDoc::redchrist_tags = !(CSwordBibleDoc::redchrist_tags);

	// write setting to the registry
	AfxGetApp()->WriteProfileInt("Tags","redchrist_tags", CSwordBibleDoc::redchrist_tags);

	SendMessageToDescendants(WM_TOGGLE_TAGS,0,0);

	// redisplay 
 	CBiblePage::ActiveMainFrameCommand = "ShowChapter";
	SendMessageToDescendants(ID_BIBLEPAGE_COMMAND, 0,0);
}

void CMainFrame::OnUpdateRed(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::redchrist_tags);
	
}



BOOL CMainFrame::OnToolTipNotify(UINT id, NMHDR *pNMHDR,   LRESULT *pResult)
{
	
   // need to handle both ANSI and UNICODE versions of the message
   TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
   TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
   CString strTipText;
   UINT nID = pNMHDR->idFrom;
   if (pNMHDR->code == TTN_NEEDTEXTA && (pTTTA->uFlags & TTF_IDISHWND) ||
      pNMHDR->code == TTN_NEEDTEXTW && (pTTTW->uFlags & TTF_IDISHWND))
   {
      // idFrom is actually the HWND of the tool
      nID = ::GetDlgCtrlID((HWND)nID);
   }

   char buf[100];

   if (nID != 0) // will be zero on a separator
   {AfxLoadString(nID,buf,100 ); strTipText.Format("%s",buf);  }

   if (pNMHDR->code == TTN_NEEDTEXTA)
      lstrcpyn(pTTTA->szText, strTipText, sizeof(pTTTA->szText));
   else
      _mbstowcsz(pTTTW->szText, strTipText, sizeof(pTTTW->szText));
   *pResult = 0;

   return TRUE;    // message was handled
}

void CMainFrame::OnClose()
{
	 if (m_bPreview)
        {		
			AfxGetMainWnd()->SendMessage(WM_COMMAND,AFX_ID_PREVIEW_CLOSE);
			return;
        }
	 
	 CFrameWnd::OnClose();

}

void CMainFrame::OnSetPreviewMode(BOOL bPreview, CPrintPreviewState* pState)
{
	m_bPreview = bPreview;
    CFrameWnd::OnSetPreviewMode(bPreview, pState);
}

// save the window placement on program exit
void CMainFrame::SavePosition()
{
	WINDOWPLACEMENT pwp;

	pwp.length = sizeof(pwp);
	GetWindowPlacement(&pwp);
	
	CString strBuffer;
	
	strBuffer.Format("%i %i %i %i %i %i %i %i %i %i",
	pwp.flags, pwp.showCmd,
	pwp.ptMinPosition.x , pwp.ptMinPosition.y , 
	pwp.ptMaxPosition.x , pwp.ptMaxPosition.y , 
	pwp.rcNormalPosition.left , pwp.rcNormalPosition.top , 
	pwp.rcNormalPosition.right , pwp.rcNormalPosition.bottom);
	
	AfxGetApp()->WriteProfileString("General","WindowPosition",strBuffer);

	//save the splitter bars
	int r1, c1, c2, temp;
	m_wndSplitter.GetRowInfo(0, r1, temp);
	m_wndSplitter1.GetColumnInfo(0, c1, temp);
	m_wndSplitter2.GetColumnInfo(0, c2, temp);



	 AfxGetApp()->WriteProfileInt("General","r1",  r1);
	 AfxGetApp()->WriteProfileInt("General","c1",  c1);	 
	 AfxGetApp()->WriteProfileInt("General","c2",  c2);	
	
}

// restore the window placement on program startup
void CMainFrame::RestorePosition()
{
	WINDOWPLACEMENT pwp;
	CString strBuffer = AfxGetApp()->GetProfileString("General","WindowPosition");

	int cRead = _stscanf(strBuffer,"%i %i %i %i %i %i %i %i %i %i",
	&pwp.flags,&pwp.showCmd,
	&pwp.ptMinPosition.x , &pwp.ptMinPosition.y , 
	&pwp.ptMaxPosition.x , &pwp.ptMaxPosition.y ,
	&pwp.rcNormalPosition.left , &pwp.rcNormalPosition.top ,
	&pwp.rcNormalPosition.right , &pwp.rcNormalPosition.bottom);
	

	if (cRead == 10) 
	{
	pwp.length = sizeof(pwp);
	SetWindowPlacement(&pwp);
		
	}

	UpdateWindow();

	//restore the splitter bars
	CRect totalSize;
	SystemParametersInfo(SPI_GETWORKAREA,0,&totalSize,0);


     int r1 =  AfxGetApp()->GetProfileInt("General","r1",  totalSize.Height());
	 int c1 =  AfxGetApp()->GetProfileInt("General","c1",  0);	 
	 int c2 =  AfxGetApp()->GetProfileInt("General","c2",  totalSize.Width());	

	 m_wndSplitter.SetRowInfo(0,  r1, 0);
	m_wndSplitter.RecalcLayout();

	m_wndSplitter1.SetColumnInfo(0, c1, 0);
	m_wndSplitter1.RecalcLayout();

	
	m_wndSplitter2.SetColumnInfo(0, c2, 0);
	m_wndSplitter2.RecalcLayout();



}

// On program exit, save the quick verse history (these are the latest verses
// that the user has typed in the quick verse toolbar

void CMainFrame::SaveQuickHistory()
{
		// write in the registry
	    CString list = "";

		int count = CSwordBibleDoc::VerseHistory.GetCount();

		if(count > MAXHISTORY) count = MAXHISTORY;

		for(int i = 0; i < count; i++)
		{
			list = list + CSwordBibleDoc::VerseHistory.GetAt(i) + "; " ;
		}

			AfxGetApp() ->WriteProfileString("QUICKVERSE", "list", list);



}

// On program startup, load the quick verse history from the registry (these are the latest verses
// that the user has typed in the quick verse toolbar
void CMainFrame::LoadQuickHistory()
{
	// get verses from the registry and add them to quick verse history array	
	 CString listStr = AfxGetApp()->GetProfileString("QUICKVERSE", "list", "");
	 sword::VerseKey vk;
	 sword::ListKey lk = vk.ParseVerseList(listStr, vk , TRUE);

	 CSwordBibleDoc::VerseHistory.RemoveAll(); //empty the array
	 
	 while(QuickVerseCombo.GetCount() > 0 )   //  empty the combo box
	 QuickVerseCombo.DeleteString(0);

	 int i = 0;
	
	 for(lk= sword::TOP; !lk.Error() && i < MAXHISTORY ; lk++)
	 {
		 i++;
		 CSwordBibleDoc::VerseHistory.Add( (const char *) lk );
		 QuickVerseCombo.AddString( (const char *) lk );
	 }


}

void CMainFrame::OnDestroy()
{
	//save window position
	 SavePosition();

	 // save quick history
	 SaveQuickHistory();

	 //save the state of the toolbars
	 CFrameWnd * pWnd = m_wndQuickVerseBar.GetDockingFrame();
	 if(pWnd->GetSafeHwnd() ) pWnd->SaveBarState("QuickVerseDockState");



	 if(pFont)
	 {
		 delete pFont;
		 pFont = NULL;
	 }

	// destroy the devotional modeless dialog box 
	if (m_pModeless)
    {
       if (::IsWindow(m_pModeless->GetSafeHwnd()))
            m_pModeless->EndDialog(IDCANCEL);
        delete m_pModeless;
			
    }
	m_pModeless = NULL;

   
	   // destroy the verselists dialog box
	if (m_pModelessVL)
    {
       if (::IsWindow(m_pModelessVL->GetSafeHwnd()))
            m_pModelessVL->EndDialog(IDCANCEL);
        delete m_pModelessVL;
			
    }
	m_pModelessVL= NULL;

	CFrameWnd::OnDestroy();

	
}



void CMainFrame::OnMainFramePrint()
{
	RECT rc;
    m_wndToolBar.GetItemRect(0,&rc);
	m_wndToolBar.ClientToScreen(&rc); // TrackPopupMenu uses screen coords

   CMenu menu;
   menu.CreatePopupMenu();
   CMenu* pPopup = NULL;
   pPopup = &menu;

   if(pPopup == NULL) return;
   
   pPopup->AppendMenu(MF_STRING,ID_BIBLE_PRINT, "Bible...");
   pPopup->AppendMenu(MF_STRING,ID_NOTES_PRINT, "Notes...");
   pPopup->AppendMenu(MF_STRING,ID_COMMENTARY_PRINT, "Commentary...");
   pPopup->AppendMenu(MF_STRING,ID_GENBOOKS_PRINT, "Generic Books...");
   pPopup->AppendMenu(MF_STRING,ID_DICTIONARY_PRINT, "Dictionary...");
  
   
   int dwSelectionMade =  pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON|TPM_RETURNCMD, 
         rc.left, rc.bottom + 2, this);

	int selection = dwSelectionMade;

	switch (selection)
	{
	case ID_BIBLE_PRINT:      OnMainBiblePrint(); break;
	case ID_COMMENTARY_PRINT: OnMFCommentaryPrint(); break;
	case ID_NOTES_PRINT:      OnNotesPrint();  break;
	case ID_DICTIONARY_PRINT: OnDictionaryPrint(); break;
	case ID_GENBOOKS_PRINT:   OnGenBooksPrint(); break;

	}

	
	pPopup->DestroyMenu(); 


}

void CMainFrame::OnMainFrameDisplay()
{
	RECT rc;
    m_wndToolBar.GetItemRect(1,&rc);
	m_wndToolBar.ClientToScreen(&rc); // TrackPopupMenu uses screen coords

   CMenu menu;
   menu.CreatePopupMenu();
   CMenu* pPopup = NULL;
   pPopup = &menu;

   if(pPopup == NULL) return;
   
   pPopup->AppendMenu(MF_STRING,ID_BIBLE_DISPLAY, "Bible...");
   pPopup->AppendMenu(MF_STRING,ID_BOTTOM_DISPLAY, "Commentary...");
   pPopup->AppendMenu(MF_STRING,ID_GENBOOKS_DISPLAY, "Generic Books...");
   pPopup->AppendMenu(MF_STRING,ID_DICTIONARY_DISPLAY, "Dictionary...");
  
   
   int dwSelectionMade =  pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON|TPM_RETURNCMD, 
         rc.left, rc.bottom + 2, this);

	int selection = dwSelectionMade;

	switch (selection)
	{
	case ID_BIBLE_DISPLAY:   OnMainDisplay(); break;
	case ID_BOTTOM_DISPLAY:  OnBottomDisplay(); break;
	case ID_DICTIONARY_DISPLAY: OnBottomleftDisplay(); break;
	case ID_GENBOOKS_DISPLAY:	OnGenBooksDisplay(); break;

	}

	
	pPopup->DestroyMenu(); 

}

void CMainFrame::OnMainFrameSearch()
{

	CRect rc;

     m_wndToolBar.GetItemRect(2,&rc);
	 m_wndToolBar.ClientToScreen(&rc); // TrackPopupMenu uses screen coords


   CMenu menu;
   menu.CreatePopupMenu();
   CMenu* pPopup = NULL;
   pPopup = &menu;

   if(pPopup == NULL) return;
   
   pPopup->AppendMenu(MF_STRING,ID_BIBLEPAGE_SEARCH, "Bible...");
   pPopup->AppendMenu(MF_STRING,ID_BOTTOM_SEARCH, "Commentary...");
    
   int dwSelectionMade =  pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON|TPM_RETURNCMD, 
         rc.left, rc.bottom + 2, this);

	int selection = dwSelectionMade;

	switch (selection)
	{
	case ID_BIBLEPAGE_SEARCH:  OnMenuBiblepageSearch(); break;
	case ID_BOTTOM_SEARCH:    OnBottomSearch();  break;
	
	}

	
	pPopup->DestroyMenu(); 


}

/////////////////////////////// Messages from main frame to the bible window /////////////////////////////



void CMainFrame::OnMainBiblePrint()
{
	CBiblePage::ActiveMainFrameCommand = "print";
	SendMessageToDescendants(ID_BIBLEPAGE_COMMAND,0,0);
}

void CMainFrame::OnMainBibleCopy()
{
	CBiblePage::ActiveMainFrameCommand = "copy";
	SendMessageToDescendants(ID_BIBLEPAGE_COMMAND,0,0);
}

void CMainFrame::OnMainBibleSelectall()
{
	CBiblePage::ActiveMainFrameCommand = "selectall";
	SendMessageToDescendants(ID_BIBLEPAGE_COMMAND,0,0);
}

void CMainFrame::OnBiblepageCopymanager()
{
	CBiblePage::ActiveMainFrameCommand = "copymgr";
	SendMessageToDescendants(ID_BIBLEPAGE_COMMAND,0,0);
}


void CMainFrame::OnMainDisplay()
{
	CBiblePage::ActiveMainFrameCommand = "display";
    SendMessageToDescendants(ID_BIBLEPAGE_COMMAND,0,0);
}


void CMainFrame::OnMainBibleZoomtext()
{
	CBiblePage::ActiveMainFrameCommand = "zoom";
	SendMessageToDescendants(ID_BIBLEPAGE_COMMAND,0,0);
}

void CMainFrame::OnUpdateBibleZoom(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::bibleZoom);
	
}

void CMainFrame::OnMenuBiblepageSearch()
{
	CBiblePage::ActiveMainFrameCommand = "search";
	SendMessageToDescendants(ID_BIBLEPAGE_COMMAND,0,0);
}

void CMainFrame::OnBibleSplitview()
{
	CBiblePage::ActiveMainFrameCommand = "split";
	SendMessageToDescendants(ID_BIBLEPAGE_COMMAND,0,0);
}

void CMainFrame::OnUpdateBibleSplitview(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck( CSwordBibleDoc::bibleSplit);
}


void CMainFrame::OnMainBiblePrevious()
{
	CBiblePage::ActiveMainFrameCommand = "previous";
	SendMessageToDescendants(ID_BIBLEPAGE_COMMAND,0,0);
}

void CMainFrame::OnMainBibleNext()
{
	CBiblePage::ActiveMainFrameCommand = "next";
	SendMessageToDescendants(ID_BIBLEPAGE_COMMAND,0,0);
}



/////////////////////////////// Messages from main frame to the commentary window /////////////////////////////


void CMainFrame::OnMFCommentaryPrint()
{
	CBottomPage::ActiveMainFrameCommand = "print";
	SendMessageToDescendants(ID_COMMENTARY_COMMAND,0,0);
}

void CMainFrame::OnMFCommentarySynch()
{
	CBottomPage::ActiveMainFrameCommand = "synch";
	SendMessageToDescendants(ID_COMMENTARY_COMMAND,0,0);
	
}

void CMainFrame::OnUpdateMFCommentarySynch(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::bottomSynch);
	
}

void CMainFrame::OnCommentaryCopy()
{
	CBottomPage::ActiveMainFrameCommand = "copy";
	SendMessageToDescendants(ID_COMMENTARY_COMMAND,0,0);
}

void CMainFrame::OnMFCommentarySelectall()
{
	CBottomPage::ActiveMainFrameCommand = "selectall";
	SendMessageToDescendants(ID_COMMENTARY_COMMAND,0,0);
}

void CMainFrame::OnBottomDisplay()
{
	CBottomPage::ActiveMainFrameCommand = "display";
	SendMessageToDescendants(ID_COMMENTARY_COMMAND,0,0);
}

void CMainFrame::OnMFCommentaryZoomtext()
{
	CBottomPage::ActiveMainFrameCommand = "zoom";
	SendMessageToDescendants(ID_COMMENTARY_COMMAND,0,0);
}

void CMainFrame::OnMFUpdateCommentaryZoom(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::bottomZoom);
}

void CMainFrame::OnBottomSearch()
{
	CBottomPage::ActiveMainFrameCommand = "search";
	SendMessageToDescendants(ID_COMMENTARY_COMMAND,0,0);
}

void CMainFrame::OnMFCommentaryPrevious()
{
	CBottomPage::ActiveMainFrameCommand = "previous";
	SendMessageToDescendants(ID_COMMENTARY_COMMAND,0,0);
}

void CMainFrame::OnMFCommentaryNext()
{
	CBottomPage::ActiveMainFrameCommand = "next";
	SendMessageToDescendants(ID_COMMENTARY_COMMAND,0,0);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////// Messages from main frame to the notes window /////////////////////////////


void CMainFrame::OnNotesPrevious()
{
	CNotesPage::ActiveMainFrameCommand = "previous";
	SendMessageToDescendants(ID_NOTES_COMMAND,0,0);
}

void CMainFrame::OnNotesNext()
{
	CNotesPage::ActiveMainFrameCommand = "next";
	SendMessageToDescendants(ID_NOTES_COMMAND,0,0);
}

void CMainFrame::OnNotesSave()
{
	CNotesPage::ActiveMainFrameCommand = "save";
	SendMessageToDescendants(ID_NOTES_COMMAND,0,0);
}

void CMainFrame::OnNotesPrint()
{
	CNotesPage::ActiveMainFrameCommand = "print";
	SendMessageToDescendants(ID_NOTES_COMMAND,0,0);
}

void CMainFrame::OnNotesSynchronize()
{
	CNotesPage::ActiveMainFrameCommand = "synchronize";
	SendMessageToDescendants(ID_NOTES_COMMAND,0,0);
}

void CMainFrame::OnUpdateNotesSynchronize(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::notesSynch);
}


void CMainFrame::OnNotesCut()
{
	CNotesPage::ActiveMainFrameCommand = "cut";
	SendMessageToDescendants(ID_NOTES_COMMAND,0,0);
}

void CMainFrame::OnNotesCopy()
{
	CNotesPage::ActiveMainFrameCommand = "copy";
	SendMessageToDescendants(ID_NOTES_COMMAND,0,0);
}

void CMainFrame::OnNotesPaste()
{
	CNotesPage::ActiveMainFrameCommand = "paste";
	SendMessageToDescendants(ID_NOTES_COMMAND,0,0);
}

void CMainFrame::OnNotesBold()
{
	CNotesPage::ActiveMainFrameCommand = "bold";
	SendMessageToDescendants(ID_NOTES_COMMAND,0,0);
}

void CMainFrame::OnUpdateNotesBold(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::notesBold);	
}

void CMainFrame::OnNotesItalic()
{
	CNotesPage::ActiveMainFrameCommand = "italic";
	SendMessageToDescendants(ID_NOTES_COMMAND,0,0);
}

void CMainFrame::OnUpdateNotesItalic(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::notesItalic);
}


void CMainFrame::OnNotesUnderline()
{
	CNotesPage::ActiveMainFrameCommand = "underline";
	SendMessageToDescendants(ID_NOTES_COMMAND,0,0);
}


void CMainFrame::OnUpdateNotesUnderline(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::notesUnderline);
}

void CMainFrame::OnNotesLeftalign()
{
	CNotesPage::ActiveMainFrameCommand = "left";
	SendMessageToDescendants(ID_NOTES_COMMAND,0,0);
}

void CMainFrame::OnUpdateNotesLeftalign(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::notesParaLeft);
}

void CMainFrame::OnNotesCenteralign()
{
	CNotesPage::ActiveMainFrameCommand = "center";
	SendMessageToDescendants(ID_NOTES_COMMAND,0,0);
}

void CMainFrame::OnUpdateNotesCenteralign(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::notesParaCenter);
}

void CMainFrame::OnNotesRightalign()
{
	CNotesPage::ActiveMainFrameCommand = "right";
	SendMessageToDescendants(ID_NOTES_COMMAND,0,0);
}

void CMainFrame::OnUpdateNotesRightalign(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::notesParaRight);
}

void CMainFrame::OnNotesInsertbullets()
{
	CNotesPage::ActiveMainFrameCommand = "bullets";
	SendMessageToDescendants(ID_NOTES_COMMAND,0,0);
}



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////// Messages from main frame to the dictionary window /////////////////////////////
void CMainFrame::OnDictionaryPrint()
{
	CBottomLeft::ActiveMainFrameCommand = "print";
	SendMessageToDescendants(ID_DICTIONARY_COMMAND,0,0);
	
}

void CMainFrame::OnDictionaryCopy()
{
	CBottomLeft::ActiveMainFrameCommand = "copy";
	SendMessageToDescendants(ID_DICTIONARY_COMMAND,0,0);

}

void CMainFrame::OnDictionarySelectall()
{
	CBottomLeft::ActiveMainFrameCommand = "selectall";
	SendMessageToDescendants(ID_DICTIONARY_COMMAND,0,0);
	
}

void CMainFrame::OnBottomleftDisplay()
{
	CBottomLeft::ActiveMainFrameCommand = "display";
	SendMessageToDescendants(ID_DICTIONARY_COMMAND,0,0);
	
}

void CMainFrame::OnDictionaryZoomtext()
{
	CBottomLeft::ActiveMainFrameCommand = "zoom";
	SendMessageToDescendants(ID_DICTIONARY_COMMAND,0,0);
}

void CMainFrame::OnUpdateDictionaryZoom(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::dictZoom);
}



void CMainFrame::OnDictionaryPrevious()
{
	CBottomLeft::ActiveMainFrameCommand = "previous";
	SendMessageToDescendants(ID_DICTIONARY_COMMAND,0,0);

}

void CMainFrame::OnDictionaryNext()
{
	CBottomLeft::ActiveMainFrameCommand = "next";
	SendMessageToDescendants(ID_DICTIONARY_COMMAND,0,0);
	
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////// Messages from main frame to the General Books Window /////////////////////////////
void CMainFrame::OnGenBooksPrint()
{
	CGenBooks::ActiveMainFrameCommand = "print";
	SendMessageToDescendants(ID_GENBOOKS_COMMAND,0,0);
	
}

void CMainFrame::OnGenBooksCopy()
{
	CGenBooks::ActiveMainFrameCommand = "copy";
	SendMessageToDescendants(ID_GENBOOKS_COMMAND,0,0);

}

void CMainFrame::OnGenBooksSelectall()
{
	CGenBooks::ActiveMainFrameCommand = "selectall";
	SendMessageToDescendants(ID_GENBOOKS_COMMAND,0,0);
	
}

void CMainFrame::OnGenBooksDisplay()
{
	CGenBooks::ActiveMainFrameCommand = "display";
	SendMessageToDescendants(ID_GENBOOKS_COMMAND,0,0);
	
}

void CMainFrame::OnGenBooksZoomtext()
{
	CGenBooks::ActiveMainFrameCommand = "zoom";
	SendMessageToDescendants(ID_GENBOOKS_COMMAND,0,0);
}

void CMainFrame::OnUpdateGenBooksZoom(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::GenBookZoom);
}



void CMainFrame::OnGenBooksPrevious()
{
	CGenBooks::ActiveMainFrameCommand = "previous";
	SendMessageToDescendants(ID_GENBOOKS_COMMAND,0,0);

}

void CMainFrame::OnGenBooksNext()
{
	CGenBooks::ActiveMainFrameCommand = "next";
	SendMessageToDescendants(ID_GENBOOKS_COMMAND,0,0);
	
}


////////////////////////////////////////////////// General Options Messages ////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void CMainFrame::OnOptionsRememberlast()
{
	CSwordBibleDoc::rememberLastVerse = !(CSwordBibleDoc::rememberLastVerse);
	
	//  store in registry
	AfxGetApp()->WriteProfileInt("General Options","Remember Last",CSwordBibleDoc::rememberLastVerse);
	
}

void CMainFrame::OnUpdateRememberlast(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::rememberLastVerse);
}

void CMainFrame::OnOptionsDevotional()
{
	 CSwordBibleDoc::devotion_startup = !(CSwordBibleDoc::devotion_startup);
	 AfxGetApp()->WriteProfileInt("Devotional","Startup",  CSwordBibleDoc::devotion_startup);
}

void CMainFrame::OnUpdateOptionsDevotional(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::devotion_startup);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CMainFrame::OnQuickVerseGo()
{
	CString keyStr = "";
	QuickVerseCombo.GetWindowText(keyStr);

	if(keyStr.GetLength() < 2) return;
		
	
	sword::VerseKey vk;
	sword::ListKey lk = vk.ParseVerseList(keyStr, vk, TRUE);
	lk = sword::TOP;
	

	if(lk.Error() || lk.Count() < 1) return;

	keyStr = lk.getShortText();
	CSwordBibleDoc::key = keyStr;
	 
	
	CBiblePage::ActiveMainFrameCommand = "ShowChapter";
	SendMessageToDescendants(ID_BIBLEPAGE_COMMAND, 0,0);

	if(CSwordBibleDoc::notesSynch)
	{
	CNotesPage::ActiveMainFrameCommand = "BibleSynch";
	SendMessageToDescendants(ID_NOTES_COMMAND,0,0);
	}

	if(CSwordBibleDoc::bottomSynch)
	{
	CBottomPage::ActiveMainFrameCommand = "BibleSynch";
	SendMessageToDescendants(ID_COMMENTARY_COMMAND,0,0);
	}

	// highlight the verse in the bible window
	CBiblePage::ActiveMainFrameCommand = "highlight";
	SendMessageToDescendants(ID_BIBLEPAGE_COMMAND,0,0);


	
	// insert string in combo box
	if(QuickVerseCombo.FindStringExact(-1, keyStr) < 0)
		QuickVerseCombo.InsertString(0, keyStr);

	else return; // the string is already there in our history

	// Add the verse to history
	CSwordBibleDoc::VerseHistory.InsertAt(0, keyStr);

	int count = CSwordBibleDoc::VerseHistory.GetCount();
	while (count > MAXHISTORY)    // VerseHistory  full
	{
		CSwordBibleDoc::VerseHistory.RemoveAt(count - 1);
		count = CSwordBibleDoc::VerseHistory.GetCount();
	}






}





